// PGLZoomEvent.cpp: implementation of the CPGLZoomEvent class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include <pgl/mfc/pglmfcinclude.h>
#include <pgl/mfc/PGLZoomEvent.h>

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CPGLZoomEvent::CPGLZoomEvent()
{

}

CPGLZoomEvent::~CPGLZoomEvent()
{

}

void CPGLZoomEvent::OnLButtonUp( UINT nFlags, CPoint point )
{
	if (!IsValid())
		return;

	if (nFlags & MK_SHIFT)
	{
		// shift is down
		GetRegion()->ZoomScale(1.5,1.5);
		GetHelper()->SetDirty();
		GetParent()->InvalidateRect(NULL,FALSE);
	}
	else if (nFlags & MK_CONTROL)
	{
		// control is down
		// shift is down
		GetRegion()->ZoomScale(0.75,0.75);
		GetHelper()->SetDirty();
		GetParent()->InvalidateRect(NULL,FALSE);
	}
};


void CPGLZoomEvent::OnMouseMove(UINT nFlags, CPoint point)
{
	int lastx,lasty;
	CPGLPointD scale;

	if (!IsValid())
		return;

	// handle left button...
	if (GetMouse()->IsLeftBtnDown(lastx,lasty))
	{
		scale.x=__min(5, __max(0.2, 1+(point.x-lastx)/(double)GetRegion()->GetView()->GetWidth()*2));
		scale.y=__min(5, __max(0.2, 1+(point.y-lasty)/(double)GetRegion()->GetView()->GetHeight()*2));

		// retreive world
		GetRegion()->ZoomScale( scale.x, scale.y);	
		//redrawing
		GetHelper()->SetDirty();
		GetParent()->InvalidateRect(NULL,FALSE);
	}
} 

BOOL CPGLZoomEvent::OnMouseWheel( UINT nFlags, short zDelta, CPoint pt )
{
	double scale;
	if (!IsValid())
		return FALSE;

	scale = __min(5, __max(0.2, 1+zDelta/(double)WHEEL_DELTA*0.25));
	// retreive world
	GetRegion()->ZoomScale(scale,scale);	
	//redrawing
	GetHelper()->SetDirty();
	GetParent()->InvalidateRect(NULL,FALSE);

	return TRUE;
};
